package com.fiberhome.ztelauncher.uilib;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.fiberhome.ztelauncher.R;
import com.fiberhome.ztelauncher.xml.bean.ViewBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xc on 2018/3/15.
 * 描述：弃用（只适用一页）
 */

public class AutoViewGroup extends FrameLayout {
    public final static int SCROLL_HORIZONTAL = 0;
    public final static int SCROLL_VERTICAL = 1;
    private List<ViewBean> viewBeanList = new ArrayList<ViewBean>();
    private int column; //列
    private int row;    //横向数量：行
    private int margin = 10;
    private int drawableShape;
    private int direction;

    public AutoViewGroup(Context context) {
        super(context);
    }

    public AutoViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = getWidth();    //父容器宽度
        int height = getHeight();  //父容器高度
        for (int i = 0; i < getChildCount(); i++) {
            ViewBean viewBean = viewBeanList.get(i);
            int leftChild = 0, topChild = 0, rightChild = 0, bottomChild = 0;
            if (direction == SCROLL_HORIZONTAL) {
                leftChild = height * viewBean.left / row;
                topChild = height * viewBean.top / row;
                rightChild = leftChild + height * viewBean.width / row;
                bottomChild = topChild + height * viewBean.height / row;
            } else {
                leftChild = width * viewBean.left / column;
                topChild = width * viewBean.top / column;
                rightChild = leftChild + width * viewBean.width / column;
                bottomChild = topChild + width * viewBean.height / column;
            }

            //遗留：靠边的间距需要修正
            if (viewBean.left == 0) {
                leftChild -= margin;
            }
            if (viewBean.top == 0) {
                topChild -= margin;
            }
            getChildAt(i).layout(leftChild + margin, topChild + margin, rightChild, bottomChild);

        }
    }

    /**
     * 添加子布局
     *
     * @param viewBeans
     * @param listener
     */
    public void addItemViews(List<ViewBean> viewBeans, final OnAddLayoutListener listener) {
        viewBeanList.clear();
        viewBeanList.addAll(viewBeans);
        removeAllViews();
        post(new Runnable() {
            @Override
            public void run() {
                int width = 0;    //父容器宽度
                int height = 0;  //父容器高度
                int widthItemView = 0;
                int heightItemView = 0;
                Log.i("TAG", "getHeight() = " + getHeight());
                Log.i("TAG", "getWidth() = " + getWidth());
                for (int i = 0; i < viewBeanList.size(); i++) {
                    ViewBean viewBean = viewBeanList.get(i);
                    if (direction == SCROLL_HORIZONTAL) {
                        height = 1692;
                        widthItemView = height / column * viewBean.width - margin * 2;
                        heightItemView = height / column * viewBean.height - margin * 2;
                        setLayoutParams(new LinearLayout.LayoutParams(height / row * column, height));
                        Log.i("TAG", "width = " + height / row * column);
                    } else {
                        width = getWidth();
                        widthItemView = width / row * viewBean.width - margin * 2;
                        heightItemView = width / row * viewBean.height - margin * 2;
                        setLayoutParams(new LinearLayout.LayoutParams(width, width / column * row));
                        Log.i("TAG", "height = " + width / column * row);
                    }
                    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(widthItemView, heightItemView);
                    lp.gravity = Gravity.CENTER;
                    lp.setMargins(margin, margin, margin, margin);
                    View view = listener.setLayout(lp.width, lp.height, i);
                    view.findViewById(R.id.parent).setLayoutParams(lp);
                    view.setLayoutParams(lp);
                    view.setBackgroundDrawable(getResources().getDrawable(drawableShape));
                    addView(view);
                }
            }
        });
    }

    /**
     * 设置子布局总数
     *
     * @param column
     * @param row
     */
    public void setGridCount(int column, int row) {
        this.column = column;
        this.row = row;
    }

    /**
     * 设置分割线宽度和颜色
     *
     * @param width
     * @param color
     */
    public void setDivide(int width, int color) {
        margin = width;
        setBackgroundColor(color);
    }

    /**
     * 设置子布局shape
     *
     * @param shape
     */
    public void setItemViewShape(int shape) {
        drawableShape = shape;
    }

    /**
     * 设置滚动方向
     *
     * @param direction
     */
    public void setScrollDirection(int direction) {
        this.direction = direction;
    }

    public interface OnAddLayoutListener {
        View setLayout(int width, int height, int position);
    }
}
